﻿Public Class FormMain
    Dim objUser As New clsUser
    Dim objCA As New clsCPLHCK
    Dim rb_checked_text As String = "2"
    Private dtCacheArea As DataTable
    Private _search_status As String
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgv.Font = New Font("UTF-8", 14)
        connectDB()
        getUserListByCJ()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_Warehouse_LH.ini"
        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub
    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(3, rb_checked_text)
        cmbOper.DisplayMember = "user_name"
        cmbOper.ValueMember = "user_id"
        cmbOper.DataSource = dtUser
    End Sub

    Private Sub getRbChechedText()
        If RadioButton4.Checked Then
            rb_checked_text = "2"
        End If
        If RadioButton5.Checked Then
            rb_checked_text = "3"
        End If
        If RadioButton6.Checked Then
            rb_checked_text = "4"
        End If
        If RadioButton1.Checked Then
            rb_checked_text = "1"
        End If
    End Sub

    Private Sub RadioButtonText_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, RadioButton6.CheckedChanged, RadioButton1.CheckedChanged
        getRbChechedText()
        getUserListByCJ()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        dtCacheArea = objCA.getList(txtProdId.Text.Trim(), cmbWHNo.Text,
                                    dt_time_start.Text.Trim(),
                                    dt_time_end.Text.Trim())
        dtCacheArea.Columns.Add("status_name")
        dtCacheArea.Columns.Add("lengthOfTime")
        Dim dtime As DateTime = getServerDateTime()
        If dtCacheArea.Rows.Count > 0 Then
            For Each row As DataRow In dtCacheArea.Rows
                If row("status") = "0" Then
                    row("status_name") = "在库"
                Else
                    row("status_name") = "已出库"
                End If
                If IsDBNull(row("out_time")) Then
                    row("lengthOfTime") = (dtime - DateTime.Parse(row("into_time"))).ToString()
                Else
                    row("lengthOfTime") = (DateTime.Parse(row("out_time")) - DateTime.Parse(row("into_time"))).ToString()
                End If
            Next
        End If
        Dim dv As DataView = dtCacheArea.DefaultView
        dv.Sort = "lengthOfTime desc"
        Dim dtCopy As DataTable = dv.ToTable()
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dtCopy
    End Sub

    Private Sub btnRk_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        Dim formRk As New FormRk()
        formRk.usr = cmbOper.Text.Trim()
        formRk.ShowDialog(Me)
    End Sub

    Private Sub btnDELIVER_Click(sender As Object, e As EventArgs) Handles btnDELIVER.Click
        Dim formDeliver As New FormDeliver()
        formDeliver.usr = cmbOper.Text.Trim()
        formDeliver.ShowDialog(Me)
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class
